//------------------------------------------------------------------------------
//!	@file	columnize.c
//!	@brief
//!		Columnize data
//!
//!	@Description
//!		Columnize the output data into a excel readable format.
//!
//!	Originally Developed: July, 1997 to October, 1999
//!
//!	$Id: columnize.c 158 2013-03-03 07:00:15Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013 A.C. Verbeck
//
//	This file is part of CreateLUT: A LUT creation library.
//
//	CreateLUT is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	CreateLUT is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with CreateLUT.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <string.h>

#include <stdint.h>
#include <stdbool.h>

#include "SysParam.h"
#include "Columnize.h"

//
//	Local defines
//
#define	MAX_LINE_LEN		8192

//
//	Local variables
//
static uint8_t	in_tab[CURVE_SZ_08BIT];
static char		out_tab[CURVE_SZ_08BIT][MAX_LINE_LEN];
static char		out_hdr[MAX_LINE_LEN];
static FILE*	in_file;
static FILE*	out_file;

//
//	Local function
//
static void Post_Curve(uint8_t* d, double gamma);
static void Init_OutputBuff(void);
static void Write_OutputBuff(FILE* f, double gain);

//
//	External data
//
extern double	StartGamma;
extern double	EndGamma;
extern int32_t	LutGammaCt;

extern double	StartGain;
extern double	EndGain;
extern int32_t	LutGainCt;

extern char InFileName[];
extern char OutFileName[];
extern char DebugFileName[];


//
//	Name:
//		Columnize
//
//	Description:
//		Converts a binary file to an excel readable file.
//
//	Parameters:
//		(None)
//
//	Returns:
//		(None)
//
void Columnize(void)
{
	int32_t	i,j;
	double	GammaIdx,GammaInc;
	double	GainIdx,GainInc;

//
//	Open binary LUT
//
	in_file = fopen(OutFileName,"rb");
	if (in_file == NULL) {
		printf("Input file open error: %s\n",OutFileName);
		exit(1);
	}

//
//	Open debug file
//
	out_file = fopen(DebugFileName,"wt");
	if (out_file == NULL) {
		printf("Output file open error: %s\n",DebugFileName);
		exit(1);
	}

//
//	Calculate the increment
//
	GainInc  = (EndGain-StartGain)/(double)(LutGainCt-1);
	GammaInc = (EndGamma-StartGamma)/(double)(LutGammaCt-1);

//
//	Dump the entire LUT
//
	for (GainIdx=StartGain,i=0; i<LutGainCt; GainIdx+=GainInc,i++) {
		Init_OutputBuff();
		for (GammaIdx=StartGamma,j=0; j<LutGammaCt; GammaIdx+=GammaInc,j++) {
			memset(in_tab, 0, CURVE_SZ_08BIT);
			fread(in_tab, 1, CURVE_SZ_08BIT, in_file);
			Post_Curve(in_tab, GammaIdx);
		}
		Write_OutputBuff(out_file, GainIdx);
	}

//
//	Close all files
//
	fclose(in_file);
	fclose(out_file);
}


//
//	Name:
//		Post_Curve
//
//	Description:
//		Posts the gamma to the header.
//		Posts the curve to the output buffer.
//
//	Parameters:
//		uint_t* d		-- buffer to post
//		double gamma	-- Gamma point
//
//	Returns:
//		(None)
//
static void Post_Curve(uint8_t* d, double gamma)
{
	uint32_t	i;
	char	CurVal[64];

	sprintf(CurVal,",%f",gamma);
	strcat(out_hdr, CurVal);

	for (i=0; i<256; i++) {
		sprintf(CurVal,",%d",d[i]);
		strcat(out_tab[i], CurVal);
	}
}


//
//	Name:
//		Columnize
//
//	Description:
//		Converts a binary file to an excel readable file.
//
//	Parameters:
//		(None)
//
//	Returns:
//		(None)
//
static void Init_OutputBuff(void)
{
	uint32_t	i;

	memset(out_hdr, 0, MAX_LINE_LEN);
	sprintf(out_hdr,"idx");

	for (i=0; i<CURVE_SZ_08BIT; i++) {
		memset(out_tab[i], 0, MAX_LINE_LEN);
		sprintf(out_tab[i],"%d",i);
	}
}


//
//	Name:
//		Columnize
//
//	Description:
//		Converts a binary file to an excel readable file.
//
//	Parameters:
//		(None)
//
//	Returns:
//		(None)
//
static void Write_OutputBuff(FILE* f, double gain)
{
	uint32_t	i;

	fprintf(f, "LUT Gain = %f \n",gain);
	fprintf(f, "%s\n", out_hdr);
	for (i=0; i<CURVE_SZ_08BIT; i++) fprintf(f, "%s\n", out_tab[i]);
	fprintf(f, "\n");
}

//
//	End of: Columnize.c
//